package com.woniu.service.impl;

import com.woniu.common.constant.Constants;
import com.woniu.common.enums.ExceptionCode;
import com.woniu.common.exception.CustomException;
import com.woniu.common.utils.token.JwtTokenUtils;
import com.woniu.entity.User;
import com.woniu.feign.UserFeignClient;
import com.woniu.model.LoginModel;
import com.woniu.service.LoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author 杨光 微信号:tolryg
 * @time 16:23
 */
@Service
public class LoginServiceImpl implements LoginService {

    private static final Logger log = LoggerFactory.getLogger(LoginServiceImpl.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserFeignClient userFeignClient;


    @Override
    public String login(LoginModel loginModel) {

        log.info("loginModel = {}", loginModel);

//        1.检查验证码是否正确
        String verifyKey = Constants.CAPTCHA_CODE_KEY + loginModel.getUuid();
        //查询redis
        String codeInRedis = stringRedisTemplate.opsForValue().get(verifyKey);
        if (!Objects.equals(codeInRedis, loginModel.getCode())) {
            throw new CustomException(ExceptionCode.INVALID_KAPTCHA);
        }
//        2.检查用户名和密码是否正确
        User user = userFeignClient.findByUserAccountAndPassword(loginModel.getUsername(), loginModel.getPassword());
        log.info("user={}", user);
        if (user == null) {
            throw new CustomException(ExceptionCode.INVALID_USERNAME_PASSWORD);

        }
//        3.生成jwt，并返回
        String jwt = JwtTokenUtils.generateJwtToken(user.getUserId(), user.getUserName(),user.getUserAccount());

        //        4.放入redis，有效期半小时
        stringRedisTemplate.opsForValue().set("token_"+user.getUserId(), jwt, 30, TimeUnit.MINUTES);

        return jwt;

    }

    @Override
    public String weblogin(LoginModel loginModel) {

        log.info("loginModel = {}", loginModel);

//        1.检查验证码是否正确
        String verifyKey = Constants.CAPTCHA_CODE_KEY + loginModel.getUuid();
        //查询redis
        String codeInRedis = stringRedisTemplate.opsForValue().get(verifyKey);
        if (!Objects.equals(codeInRedis, loginModel.getCode())) {
            throw new CustomException(ExceptionCode.INVALID_KAPTCHA);
        }
//        2.检查用户名和密码是否正确
        User user = userFeignClient.findByUserAccountAndPassword(loginModel.getUsername(), loginModel.getPassword());
        log.info("user={}", user);
        if (user == null) {
            throw new CustomException(ExceptionCode.INVALID_USERNAME_PASSWORD);

        }
//        3.生成jwt，并返回
        String jwt = JwtTokenUtils.generateWebJwtToken(user.getUserId(), user.getUserName(), user.getUserAccount());

//        4.放入redis，有效期半小时
        stringRedisTemplate.opsForValue().set("webToken_"+user.getUserId(), jwt, 30, TimeUnit.MINUTES);

        return jwt;
    }
}
